package com.itbaizhan.openhis.controller.system;

import com.itbaizhan.openhis.dto.RoleDto;
import com.itbaizhan.openhis.service.RoleService;
import com.itbaizhan.openhis.utils.ShiroSecurityUtils;
import com.itbaizhan.openhis.vo.AjaxResult;
import com.itbaizhan.openhis.vo.DataGridView;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * 角色控制器
 */
@RestController
@RequestMapping("system/role")
@Slf4j
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	
	/**
	 * 分页查询角色
	 *
	 * @param roleDto
	 * @return
	 */
	@GetMapping("listRoleForPage")
	public AjaxResult listRoleForPage(RoleDto roleDto) {
		log.info("分页查询角色");
		DataGridView dataGridView = roleService.listRoleForPage(roleDto);
		return AjaxResult.success("查询成功", dataGridView.getData(), dataGridView.getTotal());
	}
	
	/**
	 * 给用户分配角色
	 *
	 * @param userId
	 * @param roleIds
	 * @return
	 */
	@PostMapping("saveRoleUser/{userId}/{roleIds}")
	public AjaxResult saveRoleUser(@PathVariable Long userId,
								   @PathVariable Long[] roleIds) {
		log.info("保存用户和角色关系");
		if (roleIds.length == 1 && roleIds[0].equals(-1l)) {
			roleIds = new Long[]{};
		}
		roleService.saveRoleUser(userId, roleIds);
		return AjaxResult.success();
	}
	
	/**
	 * 查询所有角色
	 *
	 * @return
	 */
	@GetMapping("selectAllRole")
	public AjaxResult selectAllRole() {
		log.info("查询所有角色");
		return AjaxResult.success(roleService.listAllRoles());
	}
	
	/**
	 * 根据用户id查询角色id
	 *
	 * @param userId
	 * @return
	 */
	@GetMapping("getRoleIdsByUserId/{userId}")
	public AjaxResult getRoleIdsByUserId(@PathVariable Long userId) {
		log.info("根据用户id查询角色id,{}", userId);
		return AjaxResult.success(roleService.getRoleIdsByUserId(userId));
	}
	
	/**
	 * 添加角色
	 *
	 * @param roleDto
	 * @return
	 */
	@PostMapping("addRole")
	public AjaxResult addRole(@Validated RoleDto roleDto) {
		log.info("添加角色");
		roleDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
		return AjaxResult.toAjax(roleService.addRole(roleDto));
	}
	
	/**
	 * 修改角色
	 *
	 * @param roleDto
	 * @return
	 */
	@PutMapping("updateRole")
	public AjaxResult updateRole(@Validated RoleDto roleDto) {
		log.info("修改角色");
		roleDto.setSimpleUser(ShiroSecurityUtils.getCurrentSimpleUser());
		return AjaxResult.toAjax(roleService.updateRole(roleDto));
	}
	
	/**
	 * 批量删除角色
	 *
	 * @param roleIds
	 * @return
	 */
	@DeleteMapping("deleteRoleByIds/{roleIds}")
	public AjaxResult deleteRoleByIds(@PathVariable @Validated
									  @NotEmpty(message = "角色ID不能为空") Long[] roleIds) {
		log.info("批量删除角色,{}", roleIds);
		return AjaxResult.toAjax(roleService.deleteRoleByIds(roleIds));
	}
	
	/**
	 * 根据角色id查询角色信息
	 *
	 * @param roleId
	 * @return
	 */
	@GetMapping("getRoleById/{roleId}")
	public AjaxResult selectRoleById(@PathVariable @Validated
									 @NotNull(message = "角色ID不能为空") Long roleId) {
		log.info("查询角色,{}", roleId);
		return AjaxResult.success(roleService.getOne(roleId));
	}
	
	/**
	 * 给角色分配菜单
	 * @param roleId
	 * @param menuIds
	 * @return
	 */
	@PostMapping("saveRoleMenu/{roleId}/{menuIds}")
	public AjaxResult saveRoleMenu(@PathVariable  Long roleId,
								   @PathVariable  Long[] menuIds) {
		log.info("给角色分配菜单");
		if (menuIds.length == 1 && menuIds[0].equals(-1l)) {
			menuIds = new Long[]{};
		}
		roleService.saveRoleMenu(roleId, menuIds);
		return AjaxResult.success();
								   }
}
